// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  if (event.action && locationHelper[event.action]) {
    const result = await locationHelper[event.action](wxContext, event)
    return result
  }
  return {
    message: 'This action was not found',
    error: -1,
  }
}

const db = cloud.database()
const locationHelper = {
  // 查看所有分类
  async queryCat(context, params) {
    let res = await db.collection('cat').get()
    return res
  },
  // 添加分类
  async addCat(context,{params}){
    params.openid = context.OPENID;
    let res = await db.collection('markers').add({data:params})
    return res;
  },
  // 查看指定分类下的数据
  async queryMarkers(context, params) {
    let res = await db.collection('markers').where({
      type:params.type
    }).get()
    return res
  },
  // 根据id获取指定的数据
  async getDoc(context, {params}){
    let res = await db.collection(params.db).doc(params.id).get()
    return res;
  }
}